package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.POJO.CheckGroup;
import com.itheima.health.midPojo.Checkgroup_Checkitem;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.Checkgroup_CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupDao, CheckGroup> implements CheckGroupService {
    @Autowired
    private Checkgroup_CheckitemService ccService;

    @Autowired
    private CheckItemService checkItemService;

    @Override
    public Result findItems(Integer id) {
        //查询中建标groupid对应的所有itemid
        LambdaQueryWrapper<Checkgroup_Checkitem> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Checkgroup_Checkitem::getCheckgroupId,id);
        List<Checkgroup_Checkitem> items = ccService.list(wrapper);

        List<Long> ids=new ArrayList<>();
        for (Checkgroup_Checkitem item : items) {
            ids.add(item.getCheckitemId());
        }
//        List<CheckItem> checkItems = checkItemService.listByIds(ids);
        return new Result(true,"查询成功",ids);
    }

    @Override
    public void addItems(Integer id,Long[] checkitemIds) {
        for (Long checkitemId : checkitemIds) {
            Checkgroup_Checkitem ccItem = new Checkgroup_Checkitem(id,checkitemId);
            ccService.save(ccItem);
        }
    }

    /**
     * @description 对中间表进行修改
     * @date 2021/11/25 20:50
     * @param checkGroup:
     * @param checkitemIds:
     */
    @Override
    public Result modifyItems(CheckGroup checkGroup, Long[] checkitemIds) {
        //0.获取检查组id
        Integer id = checkGroup.getId();
        //1.更新检查组内容
        LambdaQueryWrapper<CheckGroup> wrap=new LambdaQueryWrapper<>();
        wrap.eq(CheckGroup::getId,id);
        update(checkGroup,wrap);
        //2.根据检查组id查找中间表获取检查项
        LambdaQueryWrapper<Checkgroup_Checkitem> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Checkgroup_Checkitem::getCheckgroupId,id);
        List<Checkgroup_Checkitem> items = ccService.list(wrapper);
        //3.获取检查项中所有id
        List<Long> ids=new ArrayList<>();
        for (Checkgroup_Checkitem item : items) {
            ids.add(item.getCheckitemId());
        }
        //4.对中间表进行修改，先删除后修改
        //删除
        ccService.remove(wrapper);
        //修改
        List<Checkgroup_Checkitem> ccs=new ArrayList<>();
        for (Long checkitemId : checkitemIds) {
            Checkgroup_Checkitem cc = new Checkgroup_Checkitem(id, checkitemId);
            ccs.add(cc);
        }
        ccService.saveBatch(ccs);
        return new Result(true,"编辑成功",checkGroup);
    }
}
